package com.matuw.android.adapter.recyclerview;

import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.matuw.android.adapter.ViewAdapter;
import com.matuw.android.adapter.internal.QinAdapterHelper;

import java.util.List;


public abstract class QinViewAdapter<VH extends QinViewHolder> extends RecyclerView.Adapter<VH> implements ViewAdapter<VH> {

    protected final QinAdapterHelper helper;

    public QinViewAdapter() {
        this(new QinAdapterHelper());
    }

    public QinViewAdapter(QinAdapterHelper helper) {
        this.helper = helper;
    }

    @Override
    public QinViewAdapter<VH> setDebounceClickInterval(long interval) {
        helper.setDebounceClickInterval(interval);
        return this;
    }

    @Override
    public QinViewAdapter<VH> addClickViewIds(@IdRes int... ids) {
        helper.addClickViewIds(ids);
        return this;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        VH vh = onCreateItemHolder(parent, viewType);
        helper.bindViewClickListener(this, vh, viewType);
        return vh;
    }

    @Override
    public final void onBindViewHolder(@NonNull VH holder, int position) {
        render(holder, position);
    }

    @Override
    public final void onBindViewHolder(@NonNull VH holder, int position, @NonNull List<Object> payloads) {
        render(holder, position, payloads);
    }

    @Override
    public int getItemViewType(int position) {
        return getItemType(position);
    }

    @Override
    public int getItemType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public boolean isEmpty() {
        return getItemCount() == 0;
    }

    @Override
    public void setOnItemViewClickedListener(OnItemViewClickedListener listener) {
        helper.setOnItemViewClickedListener(listener);
    }
}
